<template>
  <div>
    <div class="action-bar">
      <el-button v-if="showAddBtn" type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
    </div>
    <FormDialog title="新建" class="fixed-dialog-height" :show.sync="dialogFormVisible" width="70%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" ref="editForm" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
import { dutyInfo } from '@neu/dcom-ui/api/teamgroup'
import { myCalendar } from '@neu/dcom-ui/api/calendar'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
export default {
  name: 'HandoverActionBar',
  components: {
    FormDialog,
    EditForm
  },
  data() {
    return {
      dialogFormVisible: false,
      showAddBtn: false
    }
  },
  methods: {
    // 新建处理
    add() {
      dutyInfo().then(res => {
        if (res.data.length > 0) {
          this.dialogFormVisible = true
        } else {
          this.$message({
            showClose: true,
            message: '你不在值班范围内',
            type: 'error'
          })
        }
      })
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.$parent.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    controAddBtn() {
      const searchObj = {
        s_afterStartTime: new Date().getTime(),
        s_beforeEndTime: new Date().getTime(),
        s_isUser: true
      }
      myCalendar(searchObj).then(res => { this.showAddBtn = res.data.length !== 0 })
    }
  }

}
</script>
